/**
 * Thinking in java 
 *  2018/08/01
 */
package com.dzb.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;

/**
 * @author dzb
 * @date 2018年8月15日 下午5:14:28
 * @description  
 * @version 1.0.0
 */
public class AsyncTimeServerHandler implements Runnable{
	
	private int port =8080;
	
	CountDownLatch latch;
	
	AsynchronousServerSocketChannel asynchronousServerSocketChannel;
	
	
	/**
	 * 
	 */
	public AsyncTimeServerHandler(int port) {
		this.port=port;
		try {
			asynchronousServerSocketChannel =AsynchronousServerSocketChannel.open();
			asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
			System.out.println("初始化的端口号:"+port);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	

	@Override
	public void run() {
		latch = new CountDownLatch(1);
		doAccept();
		try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	//接收客户端消息
	public void doAccept(){
		asynchronousServerSocketChannel.accept(this, new AcceptCompletionHandler());
	}
	
}
